package master;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.BevelBorder;
import layout.TableLayout;

public class Task extends JPanel {
	private static final long serialVersionUID = 1L;
	private String taskname;
	private TaskBox box;
	private JButton finishButton;
	private Stopuhr timer;

	private Font font = new Font("Arial", Font.BOLD, 20);
	private Timer refreshTimer;
	private int status = 0;

	public Task(TaskBox box) {
		this(box, "Unbekannt", "Unbekannt");
	}

	public Task(TaskBox box, String rechnername, String name) {
		this.taskname = name;
		this.setBox(box);
		this.finishButton = new JButton("DONE");
		finishButton.setFont(font);
		finishButton.setBorder(BorderFactory.createSoftBevelBorder(BevelBorder.RAISED));
		this.finishButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getBox().remove(getTask());
			}
		});
		double[][] size = new double[][] { { 0.2, TableLayout.FILL, 0.2 },
				{ TableLayout.FILL } };
		this.setLayout(new TableLayout(size));
		this.timer = new Stopuhr();
		timer.start();
		this.setBorder(BorderFactory.createBevelBorder(1));
		JLabel rechnerLabel = new JLabel("R "+rechnername);
		JLabel taskNameLabel = new JLabel(getTaskname());
		rechnerLabel.setFont(font);
		taskNameLabel.setFont(font);
		rechnerLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		taskNameLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));

		this.add(rechnerLabel, "0,0");
		this.add(taskNameLabel, "1,0");
		this.add(finishButton, "2,0");
		this.setPreferredSize(new Dimension(50, 50));
		this.refreshTimer = new Timer(50, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				refresh();
			}
		});		
		refreshTimer.start();
	}

	protected void refresh() {
		finishButton.setText(timer.minuten()+":"+timer.sekunden());
		setColor();
	}

	private void setColor() {
		if (timer.getTime() >= getBox().getLimitTimes()[1] && status < 1) {
			status = 1;
			this.setBackground(Color.YELLOW);
		} else if (timer.getTime() >= getBox().getLimitTimes()[2] && status < 2) {
			status = 2;
			this.setBackground(Color.ORANGE);
		} else if (timer.getTime() >= getBox().getLimitTimes()[3] && status < 3) {
			status = 3;
			this.setBackground(Color.RED);
		}else if (timer.getTime() >= getBox().getLimitTimes()[4] && status < 4) {
			status = 4;
			
			Timer blinkTimer = new Timer(getBox().getBlinkSpeed(), new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					blink();
				}
			});			
			blinkTimer.start();	
		}
	}

	protected void blink() {
		if(this.getBackground()==Color.RED) {
			this.setBackground(Color.LIGHT_GRAY);
		}else{
			this.setBackground(Color.RED);
		}
	}

	public Component getTask() {
		return this;
	}

	public TaskBox getBox() {
		return box;
	}

	public void setBox(TaskBox box) {
		this.box = box;
	}

	public String getTaskname() {
		return taskname;
	}

	public void setTaskname(String taskname) {
		this.taskname = taskname;
	}

}
